package com.tcoiss.app.utils;

import org.apache.commons.collections4.ListUtils;

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

public class SplitList {
    public static String getPointsByList(List<List<Double>> list){
        List<String> points = new ArrayList<>();
        for(List<Double> point: list){
            List<String> s = new ArrayList<>();
            for(Double d:point){
                s.add(String.valueOf(d));
            }
            String pointStr = String.join(",",s);
            points.add(pointStr);
        }
        return String.join(";",points);
    }

    //切取相同的list
    public static List naiveSplitSame(List<List<Double>> originList, List<List<Double>> subList) {
        List<List<Double>> inList = new ArrayList();
        List<List<Double>> outList = new ArrayList();
        List<Integer> outIndexs = new ArrayList<>();
        List<Integer> inIndexs = new ArrayList<>();
        int index = -1;
        int index2 = -1;
        List<List<Double>> list = new ArrayList();
        for (int i = 0; i < subList.size(); i++) {
            if (originList.contains(subList.get(i))) {
                inList.add(subList.get(i));
                inIndexs.add(i);
            }else{
                outList.add(subList.get(i));
                outIndexs.add(i);
            }
        }

        if(inList.size()==originList.size()){
            return null;
        }
        int start = -1;
        int outEnd = -1;
        if(outIndexs.size()==0){
            start = subList.size()-1;
            outEnd = 0;
        }
        if(outIndexs.size()==1){
            if(outIndexs.get(0)==0){
                start = subList.size()-1;
                outEnd = 0+1;
            }else{
                start = subList.size()-2;
                outEnd = 0;
            }
        }
        if(outIndexs.size() > 1){
//            int outindex = outIndexs.get(0);
//            start = outindex-1;
//            outEnd = outIndexs.get(outIndexs.size()-2)+1;
            start = inIndexs.get(0);
            outEnd = inIndexs.get(inIndexs.size()-1);
        }
        for (int i = 0; i < originList.size(); i++) {
            if(ListUtils.isEqualList(originList.get(i),subList.get(start))){
                index = i;
            }
            if(ListUtils.isEqualList(originList.get(i),subList.get(outEnd))){
                index2 = i;
            }
        }
        if(inList.contains(originList.get(0))){
            if(index>index2){
                List<List<Double>> list1 = SplitList.naiveSplit(originList,index2,index);
                list.addAll(list1);
                list.add(originList.get(index));
                Collections.reverse(outList);
                list.addAll(outList);
            }else{
                List<List<Double>> list1 = SplitList.naiveSplit(originList,index,index2);
                list.addAll(list1);
                list.add(originList.get(index2));
//                Collections.reverse(outList);
                list.addAll(outList);
            }
        }else{
            if(index>index2){
                List<List<Double>> list1 = SplitList.naiveSplit(originList,0,index2);
                List<List<Double>> list2 = SplitList.naiveSplit(originList,index,originList.size()-1);
                list.addAll(list1);
                list.add(originList.get(index2));
//                Collections.reverse(outList);
                list.addAll(outList);
                list.addAll(list2);
                list.add(originList.get(originList.size()-1));
            }else{
                List<List<Double>> list1 = SplitList.naiveSplit(originList,0,index);
                List<List<Double>> list2 = SplitList.naiveSplit(originList,index2,originList.size()-1);
                list.addAll(list1);
                list.add(originList.get(index));
                Collections.reverse(outList);
                list.addAll(outList);
                list.addAll(list2);
                list.add(originList.get(originList.size()-1));
            }
        }
        return list;
    }
    /**

     * 原生代码拆分

     *

     * @param originList 原始list

     * @param fromIndex 拆分开始下标(包含)

     * @param toIndex 拆分结束下标(不包含)

     * @return 被拆分之后的子list

     */

    public static List naiveSplit(List originList, int fromIndex, int toIndex) {
        List subList = new ArrayList();

        if (fromIndex >= 0 && toIndex <= originList.size()) {
            for (int i = 0; i < originList.size(); i++) {
                if (i >= fromIndex && i < toIndex) {
                    subList.add(originList.get(i));

                }

            }

        }

        return subList;

    }

    /**

     * Java List.subList

     *

     * @param originList 原始list

     * @param fromIndex 拆分开始下标(包含)

     * @param toIndex 拆分结束下标(不包含)

     * @return 被拆分之后的子list

     */

    public static List subListSplit(List originList, int fromIndex, int toIndex) {
        return originList.subList(fromIndex, toIndex);

    }

    /**

     * Java 8 Collectors.partitioningBy

     *

     * @param originList 原始list

     * @param fromIndex 拆分开始下标(包含)

     * @param toIndex 拆分结束下标(不包含)

     * @return

     */

    public List partitioningBySplit(List originList, int fromIndex, int toIndex) {
        Map subList = (Map) originList.stream().collect(Collectors.partitioningBy(s -> originList.indexOf(s) >= fromIndex && originList.indexOf(s) < toIndex));

        return (List) subList.get(true);

    }

    /**

     * Java 8 Collectors.groupingBy

     *

     * @param originList 原始list

     * @param fromIndex 拆分开始下标(包含)

     * @param toIndex 拆分结束下标(不包含)

     * @return

     */

    public List groupingBySplit(List originList, int fromIndex, int toIndex) {
        Map subList = (Map) originList.stream().collect(Collectors.groupingBy(s -> originList.indexOf(s) >= fromIndex && originList.indexOf(s) < toIndex));

        return (List) subList.get(true);

    }

}
